package com.tuanzi.loan.business.repository.push.impl;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.constants.push.LoanPushMessageSendTaskStatus;
import com.tuanzi.loan.business.repository.push.custom.LoanPushMessageSendTaskCustomRepository;
import com.tuanzi.loan.core.repository.JOOQBaseRepository;
import com.tuanzi.loan.core.util.GsonUtils;
import com.tuanzi.loan.vo.request.family.push.LoanPushMessageSendTaskRequest;
import com.tuanzi.loan.vo.response.family.push.LoanPushMessageSendTaskResponse;
import com.tuanzi.server.loan_base.model.bean.push.PushMessage;
import org.jooq.Record;
import org.jooq.RecordMapper;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import static com.tuanzi.loan.generated.jooq.loan.tables.LoanPushMessageSendTask.LOAN_PUSH_MESSAGE_SEND_TASK;

/**
 * @Author: chenweijun
 * @Date: 2018/12/12 14:45
 * @Description:
 */
public class LoanPushMessageSendTaskRepositoryImpl extends JOOQBaseRepository implements LoanPushMessageSendTaskCustomRepository{

    @Override
    protected TableImpl getTable() {
        return LOAN_PUSH_MESSAGE_SEND_TASK;
    }


    @Override
    public Page<LoanPushMessageSendTaskResponse> list(LoanPushMessageSendTaskRequest searchRequest, Pageable pageable) {

        fields(LOAN_PUSH_MESSAGE_SEND_TASK.ID, LOAN_PUSH_MESSAGE_SEND_TASK.TASK_NAME, LOAN_PUSH_MESSAGE_SEND_TASK.MESSAGE,
                LOAN_PUSH_MESSAGE_SEND_TASK.PUSH_CHANNEL, LOAN_PUSH_MESSAGE_SEND_TASK.STATUS, LOAN_PUSH_MESSAGE_SEND_TASK.CREATE_TIME,
                LOAN_PUSH_MESSAGE_SEND_TASK.CREATOR, LOAN_PUSH_MESSAGE_SEND_TASK.SEND_TIME, LOAN_PUSH_MESSAGE_SEND_TASK.FINISH_TIME,
                LOAN_PUSH_MESSAGE_SEND_TASK.SEND_COUNT, LOAN_PUSH_MESSAGE_SEND_TASK.ARRIVE_COUNT, LOAN_PUSH_MESSAGE_SEND_TASK.OPEN_COUNT,
                LOAN_PUSH_MESSAGE_SEND_TASK.RESULT, LOAN_PUSH_MESSAGE_SEND_TASK.ERROR_REASON);

        contains(LOAN_PUSH_MESSAGE_SEND_TASK.TASK_NAME, searchRequest.getTaskName());
        contains(LOAN_PUSH_MESSAGE_SEND_TASK.MESSAGE, searchRequest.getContent());
        eq(LOAN_PUSH_MESSAGE_SEND_TASK.STATUS, searchRequest.getStatus());
        between(LOAN_PUSH_MESSAGE_SEND_TASK.SEND_TIME, searchRequest.getSendTimeMin(), searchRequest.getSendTimeMax());
        between(LOAN_PUSH_MESSAGE_SEND_TASK.SEND_COUNT, searchRequest.getSendCountMin(), searchRequest.getSendTimeMax());

        return search(pageable, record -> {

            PushMessage pushMessage = GsonUtils.toObject(record.get(LOAN_PUSH_MESSAGE_SEND_TASK.MESSAGE), PushMessage.class);

            LoanPushMessageSendTaskResponse response = new LoanPushMessageSendTaskResponse();
            response.setId(record.get(LOAN_PUSH_MESSAGE_SEND_TASK.ID));
            response.setTaskName(record.get(LOAN_PUSH_MESSAGE_SEND_TASK.TASK_NAME));
            response.setContent(pushMessage.getContent());
            response.setPushChannel(record.get(LOAN_PUSH_MESSAGE_SEND_TASK.PUSH_CHANNEL));
            response.setStatus(record.get(LOAN_PUSH_MESSAGE_SEND_TASK.STATUS).intValue());
            response.setCreateTime(record.get(LOAN_PUSH_MESSAGE_SEND_TASK.CREATE_TIME));
            response.setCreator(record.get(LOAN_PUSH_MESSAGE_SEND_TASK.CREATOR));
            response.setSendTime(record.get(LOAN_PUSH_MESSAGE_SEND_TASK.SEND_TIME));
            response.setFinishTime(record.get(LOAN_PUSH_MESSAGE_SEND_TASK.FINISH_TIME));
            response.setSendCount(record.get(LOAN_PUSH_MESSAGE_SEND_TASK.SEND_COUNT));
            response.setArriveCount(record.get(LOAN_PUSH_MESSAGE_SEND_TASK.ARRIVE_COUNT));
            response.setOpenCount(record.get(LOAN_PUSH_MESSAGE_SEND_TASK.OPEN_COUNT));
            response.setResult(record.get(LOAN_PUSH_MESSAGE_SEND_TASK.RESULT));
            response.setErrorReason(record.get(LOAN_PUSH_MESSAGE_SEND_TASK.ERROR_REASON));

            response.setShowCancelButton(Lists.newArrayList(LoanPushMessageSendTaskStatus.READY_SEND.ordinal(), LoanPushMessageSendTaskStatus.SENDING.ordinal())
                    .stream().anyMatch(x -> x.equals(response.getStatus())));
            response.setShowResendButton(Lists.newArrayList(LoanPushMessageSendTaskStatus.SEND_FAIL.ordinal(), LoanPushMessageSendTaskStatus.SEND_CANCEL.ordinal())
                    .stream().anyMatch(x -> x.equals(response.getStatus())));

            return response;
        });
    }

}
